package com.demoview.test.myapplication;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.squareup.picasso.Picasso;

import java.util.List;


public class DetailsStillsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>implements View.OnClickListener {
    private int mCount;
    private Context mContext;
    private List<ShopBean.DataBean.InfoBean.ListBean> dataBeans;
    private boolean isScrolling = false;

    public DetailsStillsAdapter(Context mContext, int count, List<ShopBean.DataBean.InfoBean.ListBean> dataBeans) {
        this.mContext = mContext;
        this.dataBeans =dataBeans;
        this.mCount = count;

    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.shop_item, parent, false);
        view.setOnClickListener(this);
        return new AnotherHolder(view);
    }
    private OnItemClickListener mOnItemClickListener = null;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
    @Override
    public int getItemCount() {
        return mCount;
    }


    /**
     * 设置值
     */
    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ((AnotherHolder)holder).itemView.setTag(position);
            // 这里可以用Glide等网络图片加载库
        GlideUrl glideUrl = new GlideUrl(dataBeans.get(position).getThumb(), new LazyHeaders.Builder()
                .addHeader("referer", "https://livenew.yunbaozb.com")
                .build());

            Glide
                    .with(mContext)
                    .load(glideUrl)
                    .into(((AnotherHolder)holder).picture);


        ((AnotherHolder)holder).name.setText(dataBeans.get(position).getName());
        ((AnotherHolder) holder).price.setText("￥"+dataBeans.get(position).getPrice());
        ((AnotherHolder) holder).num.setText("已售"+dataBeans.get(position).getIssale()+"件");
    }
    static class AnotherHolder extends RecyclerView.ViewHolder {
     TextView name,price,num;
     ImageView picture;

        public AnotherHolder(View itemView) {
            super(itemView);
           picture= itemView.findViewById(R.id.iv_picture);
           name= itemView.findViewById(R.id.name);
           price = itemView.findViewById(R.id.price);
           num = itemView.findViewById(R.id.num);
        }
    }
    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    public void setScrolling(boolean scrolling) {
        isScrolling = scrolling;
    }
}